package com.zhangxu.microservice.employee.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.common.utils.AesEncryptUtils;
import com.zhangxu.microservice.common.utils.AssertUtils;
import com.zhangxu.microservice.employee.biz.service.EmployeeBizService;
import com.zhangxu.microservice.employee.dao.condition.EmployeeCondition;
import com.zhangxu.microservice.employee.domain.dto.CreateEmployeeDTO;
import com.zhangxu.microservice.employee.domain.dto.EmployeeDTO;
import com.zhangxu.microservice.employee.domain.service.EmployeeDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class EmployeeBizServiceImpl implements EmployeeBizService {

    private final EmployeeDomainService employeeDomainService;
    private final PasswordEncoder passwordEncoder;

    @Override
    public Long createEmployee(CreateEmployeeDTO createEmployeeDTO) {
        EmployeeDTO employeeDTO = employeeDomainService.getByAccount(createEmployeeDTO.getAccount());
        AssertUtils.isNull(employeeDTO, "账号已存在");
        //加密密码
        String initPwd = "123456";
        String passwordCrypto = passwordEncoder.encode(initPwd);
        createEmployeeDTO.setPasswordCrypto(passwordCrypto);
        String phoneCrypto = AesEncryptUtils.encrypt(createEmployeeDTO.getPhone());
        createEmployeeDTO.setPhoneCrypto(phoneCrypto);
        return employeeDomainService.createEmployee(createEmployeeDTO);
    }

    @Override
    public void enable(Long employeeId) {
        employeeDomainService.enable(employeeId);
    }

    @Override
    public void disable(Long employeeId) {
        employeeDomainService.disable(employeeId);
    }

    @Override
    public void delete(Long employeeId) {
        employeeDomainService.delete(employeeId);
    }

    @Override
    public EmployeeDTO getByAccount(String account) {
        return employeeDomainService.getByAccount(account);
    }

    @Override
    public EmployeeDTO getById(Long id) {
        return employeeDomainService.getById(id);
    }

    @Override
    public List<EmployeeDTO> listByIds(List<Long> employeeIds) {
        return employeeDomainService.listByIds(employeeIds);
    }

    @Override
    public IPage<EmployeeDTO> employeePage(EmployeeCondition condition) {
        return employeeDomainService.page(condition);
    }
}
